﻿Imports System.Runtime.CompilerServices
Imports System.Configuration
Imports System.Text.RegularExpressions

Namespace Connection

    Public Module IDbConnectionExtenstion

        ''' <summary>
        ''' パラメータ記号を取得します。
        ''' </summary>
        <Extension()>
        Public Function GetParameterSymbol(source As IDbConnection) As String
            Dim s = KairyuSectionHandlerFactory.Create.ExecutionEnvironment.DBMS
            '#90　DBMS名を管理する
            'DBMS名から適切なパラメータ記号を返します。
            Select Case s
                Case DBMS.SqlCe, DBMS.SqlServer, DBMS.SqlServer2012
                    Return "@"
                Case DBMS.Oracle
                    Return ":"
                Case DBMS.MySQL '#97　MySQLに対応する
                    Return "?"
            End Select
            Throw New NotSupportedException(String.Format("サポートされていないDBMSです。(DBMS名：{0})", s))
        End Function

        <Extension()>
        Public Function Loader(source As IDbConnection) As Load.ModelLoader
            Return New Load.ModelLoader(source, source.GetParameterSymbol)
        End Function

        <Extension()>
        Public Function CreateCommand(source As IDbConnection, commandText As String) As IDbCommand
            Dim cmd = source.CreateCommand
            cmd.CommandText = commandText
            Return cmd
        End Function

    End Module

End Namespace

